<?php
namespace app\admin\controller;

use app\admin\controller\Base;
use think\Db;
use think\Request;

class Banji extends Base
{

    /**
     * 班级列表
     */
    public function list()
    {
        $page  = request()->param('page', '1');
        $limit = request()->param('limit', 10);
        $level = request()->param('level', '');
        $grade = request()->param('grade', '');
        $keyword = request()->param('keywords', '');
        // 总数
        $where = [];


        if ($keyword){
            $where[]=['title','like','%'.$keyword];
        }
        if ($level){
            $where[]=['level','=',$level];
        }
        if ($grade){
            $where[]=['grade','=',$grade];
        }
        $total = Db::name("Banji")->where($where)->count();
        // 搜索条件

        $list = Db::name("Banji")->where($where)->page($page, $limit)->order("id desc")->select();
        if (!empty($list)){
            foreach ($list as $key=>$val){
            }
        }
        $data = [
            'total'  => $total,
            'list'   => $list,
            'grade'=>Db::name("Yuan")->order("id desc")->column('id,title'),
            'level'=>model("Banji")::TYPE,
        ];
        return $this->ok($data);
    }


    //添加学院
    public function save()
    {
        $id=request()->param("id",'0','intval');
        $data=request()->only(['title','grade','level']);

        if (empty($id)){


            $res=Db::name("Banji")->insert($data);
        }else{
            $res=Db::name("Banji")->where(['id'=>$id])->update($data);
        }

        if ($res){
            return $this->ok('操作成功');
        }else{
            return $this->fail('操作失败');
        }
    }

    //班级详情
    public function detail()
    {
        $id=request()->param("id",'0','intval');
        $detail=Db::name("Banji")->where(['id'=>$id])->find();
        return $this->ok($detail);
    }



}
